<?php
//部门控制器
class DepartmentAction extends CommonAction {
		/**
		 * [dptList description]部门模版跳转
		 * @return [type] [description]
		 */
		public function dptList(){
			$data=$this->getAllDepartment();
			$tree=$this->createTreeStuctDpt($data);
			$json=$this->createSelectHtml($tree);
			$this->assign('json',$json);

			$this->display("Department:dptList");
		}

		/**
		 * [dptData description]获取部门json数据
		 * @return [type] [description]
		 */
		public function dptData(){
			$data=$this->getAllDepartment();
			$tree=$this->createTreeStuctDpt($data);
			$json=$this->createJsonString($tree);
			echo '['.$json.']';
		}


		/**
	     * [leaveListData description]获取请假列表
	     * @return [type] [description]
	     */
	    public function employeListData(){
	    	$result = array();
	    	$employe_name=I('employe_name');
	    	$result['total'] = $this->getListCount($employe_name);
	    	$result['rows'] = $this->getList($employe_name);
	    	echo json_encode($result);
	    }

	    /**
	     * 获取列表
	     */
	    public function getList($employe_name){
	    	$employe_id=session('employe_id');
	    	import("ORG.Util.Page");
	    	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
	    	$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
	       	$employe=M('Employe');
	       	$result=$employe->where("employe_name like '%".$employe_name."%' and employe_id!='".$employe_id."'")->page($page,$rows)->select();
	        return $result;
	    }
	    
	    /**
	     * 获取列表数量
	     */
	    public function getListCount($employe_name){
	    	$employe_id=session('employe_id');
	    	$employe=M('Employe');
	    	$result=$employe->where("employe_name like '%".$employe_name."%' and employe_id!='".$employe_id."'")->count();
	        return $result;
	    }

	    /**
	     * [findSuperiorDpt description]找出上级部门
	     * @return [type] [description]
	     */
	    public function findSuperiorDpt(){
	    	$dpt_id=I('id');
	    	$department=M('Department');
	    	$dpt_parentid=$department->where("dpt_id='".$dpt_id."'")->getField('dpt_parentid');
	    	if($dpt_parentid==0){
	    		$dpt_parentName='--请选择部门--';
	    		$dpt_parentid='';
	    	}
	    	else{
	    		$dpt_parentName=$department->where("dpt_id='".$dpt_parentid."'")->getField('dpt_name'); 
	    	}
	    	$option='<option value="'.$dpt_parentid.'" selected>'.$dpt_parentName.'</option>';
	    	$optionCheck='option[value="'.$dpt_parentid.'"]';
	    	$data=array('optionCheck' => $optionCheck ,'option' => $option);
			echo json_encode($data);
	    	//echo '<option value="'.$dpt_parentid.'">'.$dpt_parentName.'</option>';
	    }

	    /**
	     * [addDepartment description]添加部门
	     */
	    public function addDepartment(){
	    	$department=M('Department');
	    	$department->dpt_name=I('add_dpt_name');
	    	$department->dpt_parentid=I('add_dpt_parentid');
	    	$result=$department->add();
	    	if($result){
	    		echo 'ok';
	    	}
	    	else{
	    		echo 'no';
	    	}
	    }

	    /**
	     * [editDepartment description]修改部门
	     * @return [type] [description]
	     */
	    public function editDepartment(){
	    	$department=M('Department');
	    	$id=I('edit_dpt_id');
	    	$data['dpt_name']=I('edit_dpt_name');
	    	$data['dpt_parentid']=I('edit_dpt_parentid');
	    	$result=$department->where("dpt_id='".$id."'")->save($data);
	    	if($result){
	    		echo 'ok';
	    	}
	    	else{
	    		echo 'no';
	    	}
	    }

	    /**
	     * [deleteDpt description]删除该部门 以及下的所有部门
	     * @return [type] [description]
	     */
	    public function deleteDpt()
		{
			$id=I('id');
			$this->_deleteSub($id);
		}

		/**
		 * [_deleteSub description]遍历删除其子部门
		 * @param  [type] $id [description]
		 * @return [type]     [description]
		 */
		public function _deleteSub($id){
			$department=M('Department');
			$department->where("dpt_id='".$id."'")->delete();
			$dpt_id=$department->where("dpt_parentid='".$id."'")->select();
			foreach ($dpt_id as $value) {
				$this->_deleteSub($value['dpt_id']);
			}
		}

}
